<?php
class Dictionary extends DB
{
    var $_field = array('id'=>'ID');
    var $jqGridOptions = array
    (
	'datatype'	=> 'json',
	'viewrecords'	=> true,
	'pager'		=> '#data-grid-pager',
	'sortname'	=> 'id',
	'rowNum'	=> 10
    );

    function __construct()
    {
	parent::__construct();
	$this->jqGridOptions['url'] = sprintf('dictionary2.php?do=getData&table=%s',$this->_table);
    }

    function countAll()
    {
	$this->_query = sprintf('SELECT COUNT(*) FROM `%s`',$this->_table);
	if ( $this->_result = mysql_query($this->_query) )
	{
	    $data = mysql_fetch_array($this->_result);
	    return $data[0];
	}
    }

    function jqGridArray($page,$rows,$order)
    {
	$this->setLimit($rows);
	$this->setPage($page);

	$data['records']= $this->countAll();
	$data['page']	= $page;
	$data['total']	= ceil($data['records']/$rows);

	$this->_query = sprintf
	(
	    'SELECT %s FROM `%s` ORDER BY `%s` %s LIMIT %d,%d',
	    implode(',',array_keys($this->_field)),
	    $this->_table,
	    $order[0],
	    $order[1],
	    ($page-1)*$rows,
	    $rows
	);

	if ( $this->_result = mysql_query($this->_query) )
	{
	    while ( $row = mysql_fetch_array($this->_result) )
	    {
		$data['rows'][] = array('id'=>$row[0],'cell'=>$row);
	    }
	}

	return $data;
    }
}

class Status extends Dictionary
{
    var $_table = 'status';

    function __construct()
    {
	parent::__construct();
	$this->_field = array_merge($this->_field, array('status_name'=>'Статус'));

	$this->jqGridOptions = array_merge
	(
	    $this->jqGridOptions,
	    array
	    (
		'colNames'	=> array_values($this->_field),
		'colModel'	=> array
		(
		    array('name'=>'id','width'=>50),
		    array('name'=>'status_name','width'=>350),
		)
	    )
	);
    }
}

class Group extends Dictionary
{
    var $_table = 'group';

    function __construct()
    {
	parent::__construct();
	$this->_field = array_merge($this->_field, array('group_name'));
    }
}

class Manager extends Dictionary
{
    var $_table = 'manager';

    function __construct()
    {
	parent::__construct();
	$this->_field = array_merge($this->_field, array('login'=>'Логин','manager_name'=>'Менеджер','password'=>'Пароль','internal_number'=>'Внутренний номер'));

	$this->jqGridOptions = array_merge
	(
	    $this->jqGridOptions,
	    array
	    (
		'colNames'	=> array_values($this->_field),
		'sortname'	=> 'manager_name',
		'colModel'	=> array
		(
		    array('name'=>'id','width'=>50),
		    array('name'=>'login','width'=>75),
		    array('name'=>'manager_name','width'=>350),
		    array('name'=>'password','width'=>350,'sortable'=>false),
		    array('name'=>'internal_number'),
		)
	    )
	);
    }

    function auth($login,$password)
    {
	$this->_query = sprintf
	(
	    'SELECT *'
	    .' FROM `manager`'
	    .' WHERE `manager`.`login`=\'%s\''
		.' AND `manager`.`is_deleted`=0'
	    .' LIMIT 1',
	    mysql_escape_string($login)
	);

	if ( $this->_result = mysql_query($this->_query) )
	{
	    if ( ($manager = mysql_fetch_assoc($this->_result)) && is_array($manager) && $manager['password'] == $password )
	    {
		mysql_query(sprintf('INSERT INTO `arrival_time` (`manager_id`,`type`,`date_time`,`ip`) VALUES(%d,\'login\',NOW(),\'%s\')',$manager['id'],$_SERVER['REMOTE_ADDR']));

		if ( ($id = mysql_insert_id()) && is_numeric($id) )
		{
		    $manager['options'] = json_decode($manager['options'],true);

		    $this->_query = sprintf
		    (
			'SELECT `id`,TIME(date_time),ROUND((TIME_TO_SEC(TIME(MIN(`date_time`)))-TIME_TO_SEC(\'%s\'))/60),`reason`'
			.' FROM `arrival_time`'
			.' WHERE DATE(date_time)=DATE(NOW())'
			    .' AND `type`=\'login\''
			    .' AND `arrival_time`.`manager_id`=%d',
			($manager['start_time']?$manager['start_time']:constant('Common_DayStart')),
			$manager['id']
		    );

		    if ( $this->_result = mysql_query($this->_query) )
		    {
			$event = mysql_fetch_array($this->_result);

			$manager['first_login']['id']	= $event[0];
			$manager['first_login']['time']	= $event[1];
			$manager['first_login']['being']= $event[2];

			if ( !empty($event[3]) )
			{
			    $manager['first_login']['being_reason'] = $event[3];
			}
		    }

		    return $manager;
		}
	    }
	} else die(mysql_error());

	return false;
    }

    function setBeingReason($id,$reason,$being=0)
    {
	$query = sprintf('SELECT `cost` FROM `fine_settings` WHERE %d BETWEEN `delay_start` AND `delay_stop` LIMIT 1',$being);

	if ( $result = mysql_query($query) )
	    if ( $row = mysql_fetch_row($result) )
		$cost = $row[0];
	    else
		$cost = 500;
	else
	    $cost = 500;

	$this->_query = sprintf('UPDATE `arrival_time` SET `reason`=\'%s\',`cost_fine`=%0.2f WHERE `id`=%d',mysql_escape_string($reason),$cost,$id);
	if ( mysql_query($this->_query) /*&& (mysql_affected_rows()==1)*/ ) { return true; }
    }
}

class ServiceName extends Dictionary
{
    var $_table = 'service_name';

    function __construct()
    {
	parent::__construct();
	$this->_field = array_merge($this->_field, array('group_name'));
    }
}

class FieldName extends Dictionary
{
    var $_table = 'field_name';

    function __construct()
    {
	parent::__construct();
	$this->_field = array_merge($this->_field, array('group_name'));
    }
}

class Executive extends Dictionary
{
    var $_table = 'executive';

    function __construct()
    {
	parent::__construct();
	$this->_field = array_merge($this->_field, array('group_name'));
    }
}

class PhoneRate extends Dictionary
{
    var $_table = 'phone_rate';

    function __construct()
    {
	parent::__construct();
	$this->_field = array_merge($this->_field, array('group_name'));
    }

    function add($rate_name,$parent_id)
    {
	if ( !empty($rate_name) )
	{
	    mysql_query('START TRANSACTION');
	    mysql_query(sprintf('INSERT INTO `%s` (`rate_name`) VALUES (\'%s\')',$this->_table,mysql_escape_string($rate_name)));

	    if ( is_numeric($parent_id) )
	    {
		mysql_query
		(
		    sprintf
		    (
			'INSERT INTO `rate_zone` (`phone_rate_id`,`phone_direction_id`,`zone_number`,`cost`)'
			.' SELECT %d,`phone_direction_id`,`zone_number`,`cost` FROM `rate_zone`'
			    .' WHERE `phone_rate_id`=%d',
			mysql_insert_id(),
			$parent_id
		    )
		);
	    }

	    mysql_query('COMMIT');
	}
    }
}

class PhoneDirection extends Dictionary
{
    var $_table = 'phone_direction';
}

class jqRateZone extends jqGrid
{
    function getListByRate($rate_id)
    {
	if ( is_numeric($rate_id) )
	{
	    $this->_query = sprintf
	    (
		'SELECT `zone_number`,`cost`'
		.' FROM `rate_zone`'
		.' WHERE `phone_rate_id`=%d'
		.' GROUP BY `zone_number` %s %s',
		$rate_id,
		$this->_order(),
		$this->_limit()
	    );
	}
    }
}
?>
